﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;
using DetailTemplate = Cms.Domain.DetailTemplate;

namespace Cms.Web.UI.Admin
{
    public partial class DetailTemplates : AdminPage, IDetailTemplatesView
    {
        private DetailTemplatesPresenter _presenter;

        #region IDetailTemplatesView Members

        public void SetDetailTemplates(IList<DetailTemplate> detailTemplates)
        {
            rpDetailTemplates.DataSource = detailTemplates;
            rpDetailTemplates.DataBind();
            pnlNoDetailTemplates.Visible = detailTemplates.Count == 0;
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/DetailTemplates.xml"; }
        }


        public void SetReplacements(IList<DetailTemplate> detailTemplates, string name)
        {
            ddlReplacements.Items.Clear();
            foreach (DetailTemplate mt in detailTemplates)
                ddlReplacements.Items.Add(new ListItem(mt.Name, mt.ID.ToString()));
            litReplacementMessage.Text = GetText(this, "ReplacementMessage").Replace("[[Name]]", name);
            litReplacementLabel.Text = GetText(this, "ReplacementLabel").Replace("[[Name]]", name);
            upnlReplacements.Update();
            string script =
                "cms.Dialogs.Confirm(replaceDetailTemplate, 'Info', null, 'OK', 'Cancel', true, null, 'replaceDetailTemplate');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "replaceDetailTemplate", script, true);
        }


        public void DisplayUnableToDeleteAlert(string name)
        {
            string script = "cms.Dialogs.Alert(null, '" + GetText(this, "DeleteAlertHeader") + "', '" +
                            GetText(this, "DeleteAlertMessage").Replace("[[Name]]", name) + "', '" +
                            GetText(this, "DeleteAlertOK") + "');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "unableToDeleteAlert", script, true);
        }


        public void DisplayCreateMasterTemplateAlert()
        {
            string script = "cms.Dialogs.Alert(null, '" +
                            GetText(this, "CreateMasterTemplateAlertHeader") + "', '" +
                            GetText(this, "CreateMasterTemplateAlertMessage") + "', '" +
                            GetText(this, "CreateMasterTemplateAlertOK") +
                            "'); return false;";
            hlNewDetailTemplate.Attributes.Add("onclick", script);
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new DetailTemplatesPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('detailTemplatePaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }


        protected void rpDetailTemplates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var detailTemplate = (DetailTemplate) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litDescription = (Literal) e.Item.FindControl("litDescription");
            var litSystemId = (Literal)e.Item.FindControl("litSystemId");
            litID.Text = "<span id=\"D" + detailTemplate.ID + "\">";
            litName.Text = detailTemplate.Name;
            litDescription.Text = detailTemplate.Description;
            litSystemId.Text = detailTemplate.ID.ToString();
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            // TODO: Decide what to do! Is the detailtemplate in use? Is it the last one?
            _presenter.DeleteDetailTemplate(int.Parse(hidDeleteDetailTemplateID.Value.Substring(1)));
            upnlDetailTemplates.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initDetailTemplateList",
                                                            "initDetailTemplateList();", true);
        }


        protected void btnReplace_Click(object sender, EventArgs e)
        {
            if (hidReplacementID.Value != string.Empty)
                _presenter.ReplaceAndDeleteDetailTemplate(int.Parse(hidDeleteDetailTemplateID.Value.Substring(1)),
                                                           int.Parse(hidReplacementID.Value));
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initDetailTemplateList",
                                                            "initDetailTemplateList();", true);
            upnlDetailTemplates.Update();
        }
    }
}